// pages/orderlist/payorder/payorder.js
var app = getApp();
var entry;
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.order) {
      var order = JSON.parse(options.order);
      console.log(order);
      var goodsname = '';
      if (options.goodsnames) {
        goodsname = options.goodsnames;
      }
      else {
        goodsname = order.gnames;
      }
      this.setData({
        order: order,
        goodsname: goodsname
      });
      entry = '';
      if (options.entry) {
        entry = options.entry;
      }
    }
    else {
      wx.showToast({
        title: '无效订单',
        icon: 'none'
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.shareToIndex();
  },
  payOrderAction: function () {
    var url = '/index.php/api/Payment/pay_code';
    var params = {
      order_id: this.data.order.order_id
    };
    app.request(url,params).then(this.payOrderCallBack.bind(this));
  },
  payOrderCallBack: function (res) {
    if (res.status != 'ok') {
      console.log('fail支付失败');
      return;
    }
    var that = this;
    var jsapi = res.result;//JSON.parse(res.data.result);
    wx.requestPayment({
      'appId': jsapi.appId,
      'timeStamp': jsapi.timeStamp,
      'nonceStr': jsapi.nonceStr,
      'package': jsapi.package,
      'signType': 'MD5',
      'paySign': jsapi.paySign,
      'success': function (res) {
        wx.showToast({
          title: '支付成功',
          duration: 3000
        });
        var delayid = setTimeout(function () {
          clearTimeout(delayid);
          that.showOrderInfo();
        }, 3000);
      },
      'fail': function (res) {
        console.log(res);
      }
    });
  },
  waitPayOrderAction: function () {
    if (entry == 'neworder'){
      wx.redirectTo({
        url: '../orderlist2?ordertype=WAITPAY&desc=待支付订单',
      });
    }
    else{
      wx.navigateBack({});
    }
  },
  showOrderInfo:function(){
    var msg = this.data.order.consignee + '，' + this.data.order.mobile + '，' + this.data.order.address
    wx.showModal({
      title: '订单信息确认',
      showCancel: false,
      content: msg,
      confirmColor: '#e15d57',
      confirmText: '确定',
      success: function (res) {
        wx.redirectTo({
          url: '../orderlist?ordertype=3&ordercode=WAITSEND',
        });
      }
    });    
  }
})